package org.bees.cms.web.util.velocity.renderer;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import net.sf.json.JSONObject;

import org.bees.cms.web.util.Beans;

public abstract class AbstractMapRenderer extends AbstractRenderer {
	protected Map<String, String> map = new HashMap<String, String>();

	@Override
	public void initialize(JSONObject json) throws Exception {
		super.initialize(json);
		JSONObject object = json == null ? new JSONObject() : json.containsKey("map") ? json.getJSONObject("map") : new JSONObject();
		Iterator<?> iterator = object.keys();
		while (iterator.hasNext()) {
			String key = (String) iterator.next();
			String value = object.getString(key);
			map.put(key, value);
		}
	}

	@Override
	public String render(Serializable t) throws Exception {
		String key = Beans.getString(t, property);
		return "<span id='" + id + "' class='" + clazz + "' style='" + style + "'>" + map.get(key) + "</span>";
	}

	@Override
	public void destroy() throws Exception {
		super.destroy();
		map = null;
	}

	public Map<String, String> getMap() {
		return map;
	}

	public void setMap(Map<String, String> map) {
		this.map = map;
	}
}
